<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
            <el-form-item label="父级菜单">
                <el-select v-model="ruleForm.pid" placeholder="新增顶级菜单不需要选择" clearable @change="onIdSelect">
                    <template v-for="item in ids">
                        <el-option :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item label="菜单名称" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="路由路径" prop="path">
                <el-input v-model="ruleForm.path"></el-input>
            </el-form-item>
            <el-form-item label="页面文件路径" prop="component">
                <el-input v-model="ruleForm.component" placeholder="顶级菜单请填写 layout"></el-input>
            </el-form-item>
            <el-form-item label="是否显示在侧边栏">
                <el-radio v-model="ruleForm.hidden" label="0">显示</el-radio>
                <el-radio v-model="ruleForm.hidden" label="1">不显示</el-radio>
            </el-form-item>
            <!-- <el-form-item label="平台类型">
                <el-radio v-model="ruleForm.type" label="PLATFORM">平台管理端</el-radio>
                <el-radio v-model="ruleForm.type" label="MERCHANT">商家端</el-radio>
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiMenuAdd } from "@/api/admin.js";
export default {
    name: "menu_index_add",
    props: {
        ids: {
            type: Array,
            require: true
        }
    },
    data() {
        return {
            ruleForm: {
                pid: "", //父级菜单id
                sort: "",
                title: "",
                path: "",
                component: "",
                hidden: "0",
                type: "PLATFORM", // PLATFORM-平台菜单 MERCHANT-商家端菜单
            },
            rules: {
                sort: [{ required: true, message: "请输入排序", trigger: "change" }],
                title: [{ required: true, message: "请输入菜单名称", trigger: "change" }],
                path: [{ required: true, message: "请输入路径", trigger: "change" }],
                component: [{ required: true, message: "请输入页面路径", trigger: "change" }],
            },
            loading: false,
        }
    },
    methods: {
        onIdSelect(e) {
            this.ids.forEach(item => {
                if (item.id == e) {
                    this.ruleForm.sort = item.sort
                }
            });
        },
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    apiMenuAdd(this.ruleForm).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>